# -*- coding: utf-8 -*-
class LogisticsController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:newLogistics,:editLogistics,:logisticsLock]

  #物流设置页面
  def logisticsList

  end

  #显示添加物流页面
  def newLogistics

  end

  #显示编辑物流页面
  def editLogistics

  end

  def getAllLogistics
    start = params[:start]
    limit = params[:limit]
    if params[:query].nil?
      queryStr = ""
    else
      queryStr = params[:query]
    end
    
    
    logisticss = Logistics.name_like queryStr
    if start.nil?
      logisticssNow = logisticss
    else
      logisticssNow = logisticss.paginate start,limit
    end

    jsonStr="{totalProperty:"+logisticss.length.to_s+",root:"+logisticssNow.to_json(:include=>[:editor])+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #添加物流页面
  def newLogistics

  end

  def create
    begin
      logistics = Logistics.new()
      logistics.company_id = session[:company_id]
      logistics.department_id = session[:department_id]
      logistics.employee_id = session[:employee_id]
      logistics.number = params[:number]
      logistics.sub_type = params[:sub_type]
      logistics.name = params[:name]
      logistics.phone = params[:phone]
      logistics.fax = params[:fax]
      logistics.address = params[:address]
      logistics.linkman = params[:linkman]
      logistics.email = params[:email]
      logistics.bank = params[:bank]
      logistics.bank_account = params[:bank_account]
      logistics.description = params[:description]
      logistics.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  #编辑前的数据准备
  def getLogisticsById
    logistics = Logistics.find(params[:id])
    rootStr = logistics.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #把编辑后的数据，更新到数据库中
  def update
    begin
      logistics = Logistics.find(params[:id])
      logistics.number = params[:number]
      logistics.sub_type = params[:sub_type]
      logistics.name = params[:name]
      logistics.phone = params[:phone]
      logistics.fax = params[:fax]
      logistics.address = params[:address]
      logistics.linkman = params[:linkman]
      logistics.email = params[:email]
      logistics.bank = params[:bank]
      logistics.bank_account = params[:bank_account]
      logistics.description = params[:description]
      logistics.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁物流
  def logisticsLock
    logistics = Logistics.find(params[:id])
    if logistics.is_locked == 1
      logistics.is_locked = 0
    else
      logistics.is_locked = 1
    end
    logistics.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
end
